﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlatOut.Dal.Context;
using FlatOut.Dal.Domain;
using System.Data;

namespace FlatOut.Services
{
    public class ScoreService
    {
        /// <summary>
        /// Get scores by circuit
        /// </summary>
        /// <param name="circuitId"></param>
        /// <param name="typeScore"></param>
        /// <returns></returns>
        public static List<Score> GetScoresCircuit(int circuitId, TypeScore typeScore = TypeScore.Lap, byte limit = 0)
        {
            // Init
            List<Score> scores = null;

            // Get scores
            using (var context = new FlatOutContext())
            {
                scores = context.Scores
                    .Include("User")
                    .Include("Car")
                    .Include("Circuit")
                    .Where(s => s.TypeScoreInt == (int)typeScore && s.Circuit.Id == circuitId).ToList()
                    .OrderBy(s => s.Time).ToList();

                // Limit
                if (limit > 0)
                    scores = scores.Take(limit).ToList();
            }

            return scores;
        }

        /// <summary>
        /// Get scores by circuit by user
        /// </summary>
        /// <param name="circuitId"></param>
        /// <param name="typeScore"></param>
        /// <returns></returns>
        public static List<Score> GetScoresUser(int userId, TypeScore typeScore = TypeScore.Lap)
        {
            // Init
            var scores = new List<Score>();

            // Get scores
            using (var context = new FlatOutContext())
            {
                // Get circuits
                var circuits = context.Circuits.ToList();

                // ForEach circuits, get Best score
                foreach (var circuit in circuits)
                {
                    var bestTime = GetScoresCircuit(circuit.Id, typeScore, 1).First();

                    // If bestScore is made by user, add it
                    if (bestTime.UserId == userId)
                        scores.Add(bestTime);
                }

                /*scores = context.Scores
                    .Include("User")
                    .Include("Car")
                    .Where(s => s.TypeScoreInt == (int)typeScore && s.User.Id == userId)
                    .OrderBy(s => s.Time).ToList();
                 **/
            }

            return scores;
        }       


        /// <summary>
        /// Find a score in base
        /// </summary>
        /// <param name="score"></param>
        public static Score Find(int id)
        {
            Score score = null;

            using (var context = new FlatOutContext())
            {
                score = context.Scores.Find(id);
            }

            return score;
        }


        /// <summary>
        /// Add a score in base
        /// </summary>
        /// <param name="score"></param>
        public static void Add(Score score)
        {
            using (var context = new FlatOutContext())
            {
                context.Scores.Add(score);
                context.SaveChanges();
                
            }
        }


        /// <summary>
        /// Update a score in base
        /// </summary>
        /// <param name="score"></param>
        public static void Update(Score score)
        {
            using (var context = new FlatOutContext())
            {
                context.Entry(score).State = EntityState.Modified;
                context.SaveChanges();
            }
        }


        /// <summary>
        /// Delete a score in base
        /// </summary>
        /// <param name="score"></param>
        public static void Delete(int id)
        {
            using (var context = new FlatOutContext())
            {
                Score score = context.Scores.Find(id);
                context.Scores.Remove(score);
                context.SaveChanges();
            }
        }


        #region Privates methods
        /// <summary>
        /// Get Scores
        /// </summary>
        /// <returns></returns>
        private static List<Score> GetScores()
        {
            // Init
            List<Score> scores = null;

            // Get scores
            using (var context = new FlatOutContext())
            {
                scores = context.Scores
                    .Include("User")
                    .Include("Car")
                    .OrderBy(s => s.Time).ToList();
            }

            return scores;
        }
        #endregion
    }
}